package com.huan.iteye.analysis.support.impl;

import com.google.common.collect.Maps;
import com.huan.iteye.analysis.domain.IteyeBlogView;
import com.huan.iteye.analysis.repository.IteyeBlogViewRepository;
import com.huan.iteye.analysis.support.AbstractBlogAnalysisSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 前天最高访问的博客统计
 *
 * @author huan.fu
 * @date 2018/9/18 - 14:07
 */
@Component
@Order(3)
public class TheDayBeforeYesterdayViewTopNAnalysis extends AbstractBlogAnalysisSupport {

	@Autowired
	private IteyeBlogViewRepository iteyeBlogViewRepository;

	@Override
	public String doAnalysis(List<String> workDays) {
		// 获取最近的2天时间
		List<String> analysisDays = Arrays.asList(workDays.get(workDays.size() - 1), workDays.get(workDays.size() - 2));
		// key:日期 value:博客的访问量
		Map<String, List<IteyeBlogView>> everyDayViewList = iteyeBlogViewRepository.findByWorkDateIn(analysisDays)
				.stream()
				.collect(Collectors.groupingBy(IteyeBlogView::getWorkDate));

		// 日期  博客  访问量
		Map<String, Map<String, Long>> everyDayEachBlogViewMap = new HashMap<String, Map<String, Long>>(everyDayViewList.size() * 2) {
			@Override
			public Map<String, Long> get(Object key) {
				Map<String, Long> value = super.get(key);
				if (null == value) {
					value = Maps.newHashMap();
					super.put((String) key, value);
				}
				return value;
			}
		};

		// 日期  博客  访问量
		everyDayViewList.forEach((key, value) -> value.forEach(blogView -> everyDayEachBlogViewMap.get(key).putIfAbsent(blogView.getTitle(), blogView.getViewCnt())));

		// 博客  访问量
		Map<String, Long> topBlogViewMap = Maps.newHashMap();

		// 得到当前时间后一天的时间
		Map<String, Long> first = everyDayEachBlogViewMap.get(analysisDays.get(0));
		// 得到当前时间后两天的时间
		Map<String, Long> second = Optional.ofNullable(everyDayEachBlogViewMap.get(analysisDays.get(1))).orElseGet(HashMap::new);
		first.forEach((title, views) -> {
			// 如果后两天中不存在后一天的博客，那么默认访问量设置成0
			Long secondView = Optional.ofNullable(second.get(title)).orElse(0L);
			topBlogViewMap.putIfAbsent(title, views - secondView);
		});

		// 按照访问量从高到低进行排序
		List<Map.Entry<String, Long>> topNEntry = topBlogViewMap.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByValue((o1, o2) -> o2.intValue() - o1.intValue()))
				.collect(Collectors.toList());

		return generatorHtml(topNEntry, analysisDays.get(1));
	}

	/**
	 * 生成 html 代码
	 *
	 * @param topNEntry
	 * @param workDate
	 * @return
	 */
	public String generatorHtml(List<Map.Entry<String, Long>> topNEntry, String workDate) {
		StringBuilder builder = new StringBuilder(500);
		builder.append("<table>");
		builder.append("<caption><h3>").append("前天各篇博客访问量").append("</h3></caption>");
		builder.append("<thead>")
				.append("<tr>")
				.append("<th>").append("业务日期").append("</th>")
				.append("<th>").append("博客标题").append("</th>")
				.append("<th>").append("增量访问量").append("</th>")
				.append("</tr>")
				.append("</thead>")
				.append("<tbody>");

		topNEntry.forEach(entry -> {
			builder.append("<tr>")
					.append("<td>").append(workDate).append("</td>")
					.append("<td>").append(entry.getKey()).append("</td>")
					.append("<td>").append(entry.getValue()).append("</td>")
					.append("</tr>");
		});

		builder.append("</tbody>");
		builder.append("</table>");
		return builder.toString();
	}
}
